<?php
/**
 * the tag
 * @uses Blib_Data_Collection
 */
class Blib_View_Tag extends Blib_Data_Collection
{
    /**
     * start with a tag name
     * @param str $name the tag name
     * @return void
     */
    public function __construct($name)
    {
        parent::__construct(array(
            'tag' => $name,
            'attr' => array(),
            'value' => null
        ));

    } // end __construct


    // play with string
    private function _str($ex, $new, $type = 'SET', $sep = ' ')
    {
        // DO NOT USE SWITCH TO SPEED UP THE PROCESS
        if ($type == 'SET' || empty($ex)) {
            return $new;
        }
        if ($type == 'APPEND') {
            return $ex . $sep . $new;
        }
        if ($type == 'PREPEND') {
            return $new . $sep . $ex;
        }

    }// end _str


    /**
     * change tag name
     * @param str $val if empty return the current tag name
     * @return mixed
     */
    public function name($val = null)
    {
        if (!empty($val)) {
            $this->add('tag', $val);
            return $this;
        }

        return $this->get('tag');

    }// end name


    /**
     * add tag attributes
     * @param str $k the key can be string or array
     * @param str $v the value
     * @param str $type the type of value
     * @param str $sep the separator
     * @return self
     */
    public function attr($k, $v = null, $type = 'SET', $sep = ' ')
    {
        if (is_array($k)) {
            $this->add('attr', $k);
            return $this;
        }

        if (is_string($k) && empty($v)) {
            // retrieve the attr
            if (isset($this->_data['attr'][$k])) {
                return $this->_data['attr'][$k];
            }
            return null;
        }

        $this->_data['attr'][$k] = $this->_str($this->attr($k), $v, $type, $sep);
        return $this;

    }// end attr


    /**
     * remove attributes
     * @param str $k the key
     * @return self
     */
    public function removeAttr($k)
    {
        if (isset($this->_data['attr'][$k])) {
            unset($this->_data['attr'][$k]);
        }
        return $this;

    }// end removeAttr


    /**
     * tag value
     * @param str $val the value
     * @param str $type the type of value
     * @param str $sep the separator
     * @return self
     */
    public function value($val = null, $type = 'SET', $sep = ' ')
    {
        if (empty($val)) {
            // default is to return the value
            return $this->get('value');
        }

        return $this->add('value', (string) $this->_str($this->get('value'), $val, $type, $sep));

    }// end value


    /**
     * so you can echo tag directly
     */
    public function __toString()
    {
        return $this->export();

    }// end __toString


    /**
     * export
     * @param bool $raw if true, return it only
     * @return mixed
     */
    public function export($raw = true)
    {
        $name = $this->get('tag');
        $tag  = '<' . $name;
        foreach ((array) $this->get('attr') as $k => $v) {
            $tag .= ' ' . $k . '="' . $v . '"';
        }
        $val = $this->get('value');
        if (!empty($val)) {
            $tag .= ">{$val}</{$name}>";
        } else {
            $tag .= " />";
        }

        if ($raw) {
            return $tag;
        }

        echo $tag;
        return $this;

    }// end export

} // end Blib_View_Tag
?>